package oj;

import java.util.PriorityQueue;
import java.util.Queue;

public class LC295 {
    Queue<Integer> maxHeap;
    Queue<Integer> minHeap;
    /** initialize your data structure here. */
    public LC295() {
        maxHeap=new PriorityQueue<>((x, y)->(y-x));//存储较小元素的一半
        minHeap=new PriorityQueue<>();//存储较大元素的一半
    }

    public void addNum(int num) {
        if(maxHeap.size()!=minHeap.size()){
            minHeap.add(num);
            maxHeap.add(minHeap.poll());
        }else{
            maxHeap.add(num);
            minHeap.add(maxHeap.poll());
        }
    }

    public double findMedian() {
        return maxHeap.size()!=minHeap.size()?minHeap.peek():(minHeap.peek()+maxHeap.peek())/2.0;
    }
}
